/*
 * @Author: 王鹏 2911783749@qq.com
 * @Date: 2022-10-20 09:21:30
 * @LastEditors: 王鹏 2911783749@qq.com
 * @LastEditTime: 2022-10-26 14:43:12
 * @FilePath: \v_shop\code\src\router\index.js
 * @Description: 路由表
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../components/Login.vue')
  },
  {
    path: '/home',
    name: 'home',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../components/Home.vue'),
    children:[
      {
        path:'',
        redirect:'index'
      },
      {
        path:'index',
        name:'index',
        component:()=>import('../components/children/Index')
      },
      {
        path:'users',
        name:'users',
        component:()=>import('../components/children/Users')
      },
      {
        path:'goods',
        name:'goods',
        component:()=>import('../components/children/Goods')
      },
      {
        path:'roles',
        name:'roles',
        component:()=>import('../components/children/Roles')
      },
      {
        path:'rights',
        name:'rights',
        component:()=>import('../components/children/Rights')
      },
      {
        path:'categories',
        name:'categories',
        component:()=>import('../components/children/Cate')
      },
    ]
  },
]

const router = new VueRouter({
  routes
})
// 全局前置路由守卫
// router.beforeEach((to,from,next)=>{
//   const isLogin=sessionStorage.token ? true : false
//   if(to.path=='/login'){
//     next()
//   }else{
//     isLogin ? next() : next('/login')
//   }
// })

router.beforeEach((to,from,next)=>{
  const isLogin =sessionStorage.getItem('token') ? true : false
  if(to.path=="/login"){
    next()
  }else{
    isLogin ? next() :next('/login')
  }
})

export default router
